<?php
// Replace with your OpenAI API key
$openai_api_key = 'YOUR_API_KEY';

// Get prompt and image name from form data
$prompt = $_POST['prompt'];
$size =  $_POST['size'];
// Call the OpenAI API to create an image
$curl = curl_init();
    CURLOPT_URL => "https://api.openai.com/v1/images/generations",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode([
        'model' => 'image-alpha-001',
        'prompt' => $prompt,
        'num_images' => 1,
        'size' => "{$size}",
        'response_format' => 'url'
    ]],
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer $openai_api_key",
        "Content-Type: application/json"
    ],
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}
